//
//  FaxianController.m
//  网易新闻
//
//  Created by dllo on 15/10/5.
//  Copyright (c) 2015年 Lx. All rights reserved.
//

#import "FaxianController.h"
#import "MJRefresh.h"
#import "AFTool.h"
#import "FaxianCell.h"
#import "FaxianModel.h"
#import "WebController.h"
#import "Settings.h"
@interface FaxianController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSArray *modArr;
@property(nonatomic, assign)NSInteger cellheight;
@property(nonatomic, retain)UILabel *naviTitle;
@end
@implementation FaxianController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.cellheight = 44;
        self.modArr = [[NSMutableArray alloc]init];
        [self createTableView];
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    self.tabBarController.tabBar.barTintColor = [[Settings shareSettings]color:WhiteToGray];
    self.tabBarController.tabBar.hidden = NO;
    [self.naviTitle setHidden:NO];
    [self handelData];
}

- (void)viewWillLayoutSubviews{
    self.tableView.contentOffset = CGPointMake(0, 0);
    [self.tableView.header beginRefreshing];
    
    self.naviTitle = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, 75, 25)];
    self.naviTitle.textColor = [UIColor whiteColor];
    self.naviTitle.text = @"发现";
    [self.navigationController.navigationBar addSubview:self.naviTitle];
    [self.naviTitle release];
}

- (void)viewWillDisappear:(BOOL)animated{
    [self.naviTitle setHidden:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
    [self notificationHandle];
}

- (void)createTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 65, 375, 560) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [[Settings shareSettings]color:WhiteToGray];;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView release];
    
    [self.tableView registerClass:[FaxianCell class] forCellReuseIdentifier:@"FaxianCell"];
    self.tableView.tableFooterView = [[UIView alloc]init];
    
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
    }];
}

- (void)handelData{
    NSString *url = @"http://c.m.163.com/nc/topicset/uc/api/discovery/indexV52";
    [AFTool GETWithURL:url body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        NSDictionary *tempDic = [[NSDictionary alloc]initWithDictionary:result];
        self.modArr = [[NSArray alloc]initWithArray:[tempDic objectForKey:@"stream"]];
        [[Settings shareSettings]writeToDBWithDic:tempDic WithName:url];
        [tempDic release];
        [self.tableView reloadData];
    } fail:^(NSError *error) {
        NSDictionary *tempDic = [[NSDictionary alloc]initWithDictionary:[[Settings shareSettings]dicSelectFromDB:url]];
        if (![tempDic isEqual:nil]) {
            self.modArr = [[NSArray alloc]initWithArray:[tempDic objectForKey:@"stream"]];
            [tempDic release];
            [self.tableView reloadData];
        }
        NSLog(@"404(FaxianCellController)");
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return self.modArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FaxianCell *cell = [[FaxianCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FaxianCell"];
    FaxianModel *mod = [FaxianModel modWithDic:self.modArr[indexPath.row]];
    cell.open = ^(NSString *url){
        WebController *web = [[WebController alloc]init];
        web.url = url;
        [self.navigationController pushViewController:[web autorelease] animated:YES];
    };
    cell.mod = mod;
    self.cellheight = cell.height;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.cellheight;
}

- (void)notificationHandle{
    [[NSNotificationCenter defaultCenter]addObserverForName:@"changeBackgroundColor" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
        self.view.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
        self.tableView.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
        self.tabBarController.tabBar.barTintColor = [[Settings shareSettings]color:WhiteToGray];
    }];
}

- (void)dealloc{
    _tableView.delegate = nil;
    _tableView.dataSource = nil;
    [_modArr release];
    [_tableView release];
    [_naviTitle release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end